﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace Acoris.Core
{
    using Microsoft.Graphics.Canvas.Text;

    /// <summary>
    /// 表示系统的字体枚举器
    /// </summary>
    /// <remarks>当前控件的实现目前只能用于Windows10操作系统</remarks>
    public sealed class FontFamilyControl : Control
    {
        #region FontFamilyControlCommand Defination

        class FontFamilyControlCommand : ICommand
        {
            Action<object> m_method;
            public FontFamilyControlCommand(Action<object> method)
            {
                m_method = method ?? throw new NullReferenceException();
            }

            public event EventHandler CanExecuteChanged;

            public bool CanExecute(object parameter) => true;

            public void Execute(object parameter) => m_method(parameter);

            public void RaiseCanExecute()
            {
                CanExecuteChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        #endregion

        #region Constructor

        public FontFamilyControl()
        {            
            foreach(var element in GetFontFamilys()) {
               FontFamilyCollection.Add(element);
            }
            Refresh = new FontFamilyControlCommand(RefreshFontFamily);
            this.DefaultStyleKey = typeof(FontFamilyControl);
        }

        #endregion

        #region Method

        /// <summary>
        /// 枚举系统的字体
        /// </summary>
        /// <returns>返回一个可进行简单迭代的枚举数</returns>
        public static IEnumerable<FontFamily> GetFontFamilys()
        {
            var collection = new  List<FontFamily>();
            var hashSet = new HashSet<string>();
            // get system font set
            var fontSet = CanvasFontSet.GetSystemFontSet();
            var fonts = fontSet.Fonts;
            // it represent a font file
            // maybe there are some subfontFamily
            // so we need enumerate it
            foreach (var font in fonts) {
                foreach (var family in font.FamilyNames) {
                    var pair = family;
                    // pair.Key ,its value represents a language
                    // pair.value,its value represents a FamilyName   
                    hashSet.Add(pair.Value);
                }
            }

            foreach(var fontFamily in hashSet) {
                collection.Add(new FontFamily(fontFamily));
            }

            return collection;
        }

        /// <summary>
        /// 刷新系统中的字体集合
        /// </summary>
        /// <param name="param"></param>
        private void RefreshFontFamily(object param)
        {
            FontFamilyCollection.Clear();
            var enumerable = GetFontFamilys();
            foreach(var item in enumerable) {
                FontFamilyCollection.Add(item);
            }
        }

        #endregion

        #region RefreshCommand

        /// <summary>
        /// 表示刷新命令
        /// </summary>
        public ICommand Refresh {
            get { return (ICommand)GetValue(RefreshProperty); }
            private set { SetValue(RefreshProperty, value); }
        }


        public static readonly DependencyProperty RefreshProperty =
        DependencyProperty.Register("Refresh", typeof(ICommand), typeof(FontFamilyControl),
            new PropertyMetadata(null));

        #endregion

        #region CurrentSelection

        public FontFamily SelectedFontFamily {
            get { return (FontFamily)GetValue(SelectedFontFamilyProperty); }
            set { SetValue(SelectedFontFamilyProperty, value); }
        }

        
        public static readonly DependencyProperty SelectedFontFamilyProperty =
        DependencyProperty.Register("SelectedFontFamily", typeof(FontFamily), typeof(FontFamilyControl), new PropertyMetadata(null));

        #endregion

        #region FontFamilyCollection

        public ObservableCollection<FontFamily> FontFamilyCollection {
            get { return (ObservableCollection<FontFamily>)GetValue(FontFamilyCollectionProperty); }
            private set { SetValue(FontFamilyCollectionProperty, value); }
        }
        
        public static readonly DependencyProperty FontFamilyCollectionProperty =
        DependencyProperty.Register("FontFamilyCollection", typeof(ObservableCollection<FontFamily>), typeof(FontFamilyControl), new PropertyMetadata(new ObservableCollection<FontFamily>()));

        #endregion

    }
}
